<?php
$loader->requireOnce('includes/inPatientCore/InPatientCoreErrorCode.class.php');
$loader->requireOnce('includes/inPatientCore/InPatientCoreException.class.php');
$loader->requireOnce('includes/inPatientCore/InPatientSEBase.class.php');
$loader->requireOnce('ordo/InPatient.class.php');
$loader->requireOnce('ordo/EmployeeType.class.php');
$loader->requireOnce('ordo/Service.class.php');



/**
 * InPatientEctomy Processor.
 * @package core.includes.inPatientCore
 * @author viethd
 * @version $Id$
 * @copyright 2009 LHV Company
 */
class InPatientEctomy extends InPatientSEBase{
	private $logger ;


	/**
	 *
	 * @param $controller controller for build ui
	 */
	function __construct($controller) {
		parent::__construct($controller);
		$this->logger = LoggerManager::getLogger('core.inPatientCore.InPatientEctomy');
	}
	
	public function loadInputForm($ticketId) {
		$path = $this->controller->view->path;
		$employeeObj = Celini::newORDO("Employee");
		$doctorList = $employeeObj->getEmployeeOfDepartment($this->controller->_me->get_department_id(),EmployeeType::DOCTOR_ID, true);
		$emr = Celini::newORDO("InPatientEmr");
        $emrInfo =  $emr->getEmrInfo($ticketId);
        
		$surgeryObj = Celini::newORDO("Surgery");
		$classify = Utils::getEnumKey(EnumerationConstant::SURGERY_CLASSIFY, EnumerationConstant::SURGERY_CLASSIFY_THUTHUAT);
		$surgeriesEmrDetail = $surgeryObj->getSurgeryEmrDetailInfo($this->controller->_me->get_department_id(), $emrInfo['in_patient_emr_id']);
		$tbody = $this->buildEctomyEmrDetailTable($ticketId, $surgeriesEmrDetail);
	    $this->controller->view->path = "inpatient_surgery";
        $orders =   L10nHelper::getYMDorder();             
        $this->controller->assign('ticketId', $ticketId);
        $this->controller->assign('doctorList', $doctorList);
        $this->controller->assign('tbody', $tbody);
        $form = $this->controller->view->render('input_ectomy.html');
        $this->controller->view->path = $path;
        return $form;
	}
	
	
	private function buildEctomyEmrDetailTable($ticketId, $surgeriesEmrDetail) {
		$html = "";
		foreach ($surgeriesEmrDetail as $row) {
			$html .= "<tr id = 'surgery". $row['surgery_id'] . "'>";
			$html .= "<td>";
			$html .= $row['date'];
			$html .= "</td>";
			
			$html .= "<td>";
			$html .= $row['name'];
            $html .= "</td>";
            
            $html .= "<td>";
            $html .= L10nHelper::renderNumber($row['original_price'],0);
            $html .= "</td>";
            
            $html .= "<td>";
            $html .= L10nHelper::renderNumber($row['insurance_price'],0);
            $html .= "</td>";
            
            $html .= "<td>";
            $html .= L10nHelper::renderNumber($row['patient_price'],0);
            $html .= "</td>";
            
            $html .= "<td>";
            
            if (isset($row['participants']) && !empty($row['participants'])) {
                $html .= $row['participants'][0]['name'];
            }
            
            $html .= "</td>";
            
            $html .= "<td>";
            $html .= "<a href=\"javascript:deleteInPatientEctomy('$ticketId','" . $row['surgery_id'] . "')\"> <img src='../images/remove_small.png' > </a>";
            $html .= "</td>";
            
            $html .= "</tr>";
		}
		return $html;
		
	}
    public function deleteInPatientEctomy($ticketId, $surgeryId) {

        $participant = Celini::newOrdo('SurgeryParticipant');
        $participant->deleteParticipants($surgeryId);

        $surgeryObj = Celini::newOrdo('Surgery');
        $surgeryObj->deleteSurgery($surgeryId);
        $itemType = Utils::getEnumKey(EnumerationConstant::IN_TICKE_ITEM_ITEMTYPE,
        EnumerationConstant::IN_TICKE_ITEM_ITEMTYPE_SURGERY);
        $inTicketItemObj = Celini::newOrdo('InTicketItem');
        $inTicketItemObj->deleteItem($surgeryId, $itemType);


    }
    public function addEctomyService($ticketId, $surgeryServiceId, $surgeryDate, $doctorId) {
        $emr = Celini::newORDO("InPatientEmr");
        $emrInfo =  $emr->getEmrInfo($ticketId);

        $surgeryObj = Celini::newOrdo('Surgery');
        $surgeryObj->setData($emrInfo['in_patient_emr_id'], $surgeryDate, '', '',
        '', '', $surgeryServiceId, '');
        $surgeryObj->persist();
        $surgeryId = $surgeryObj->get_id();
        $serviceObj = Celini::newOrdo('Service');
        $prices = $serviceObj->getPrices($ticketId, $surgeryServiceId, EnumerationConstant::SER_INS_ITEM_TYPE_PTTT);

        $participant = Celini::newOrdo('SurgeryParticipant');
        $role = Utils::getEnumKey(EnumerationConstant::SURGERY_PARTICIPANT_ROLE, 
        EnumerationConstant::SURGERY_PARTICIPANT_ROLE_SUR);
        $participant->setData($surgeryId, $doctorId, $role);
        $participant->persist();
        $itemType = Utils::getEnumKey(EnumerationConstant::IN_TICKE_ITEM_ITEMTYPE,
        EnumerationConstant::IN_TICKE_ITEM_ITEMTYPE_SURGERY);
        $inTicketItemObj = Celini::newOrdo('InTicketItem');
        $inTicketItemObj->setData($surgeryId,
        $itemType,
        0,$prices['price'],$prices['insurance_price'], $prices['patient_price'], $ticketId, null);
        $inTicketItemObj->persist();

    }
	
}
?>